/**
  ******************************************************************************
  * File Name          : ModbusSlave.c
  * Description        : 本文档包含easy_modbus协议的宏定义、变量声明、函数原型等 
	*本MODBUS协议从站处理文档，不处理与CRC校验相关的事务
	*对于CRC，需要用户在主函数进行处理，如CRC校验通过，则通过本文档处理MODBUS协议指令处理
  *
  ******************************************************************************
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "ModbusSlave.h"
//#include "config.h"



/* Exported typedef ------------------------------------------------------------------*/


/* MODBUS协议使用的结构体 ------------------------------------------------------------------*/


E_MBUS_DataTypeDef				E_MBUS_DEV_WRREG;

E_MBUS_BufDataTypeDef 		E_MBUS_BUF_DATA_ALL;



/*MODBUS默认参数*/

E_MBUS_DataTypeDef E_MBUS_DATA_ALL=
{
		//X
		{E_MBUS_X_PIN_RESET},
		
		//Y
		{E_MBUS_Y_PIN_RESET},
		
		//mbus_rreg
		{0},
		
		//mbus_wrreg0
		{0},
		//mbus_wrreg1
		{0},
		
		//mbus_id
		1
};


/* Function ------------------------------------------------------------------*/

/*01功能码：读连续空间的线圈状态----------------------------*/
/*buf[0]=ID;buf[1]=CMD;buf[2]=size;buf[3]=addrsize*/
E_MBUS_StatusTypeDef E_MBUS_Y_READ(E_MBUS_BufDataTypeDef *e_mbus_bufdata)
{
	//变量定义
//	E_MBUS_StatusTypeDef recive_status=E_MBUS_ERROR_OTHER;
	E_MBUS_DATA for_i;
	
	//获取起始地址和数据数量
	e_mbus_bufdata->mbus_start=((e_mbus_bufdata->m_bus_rxbuf[2])<<8)|(e_mbus_bufdata->m_bus_rxbuf[3]);
	e_mbus_bufdata->mbus_size= ((e_mbus_bufdata->m_bus_rxbuf[4])<<8)|(e_mbus_bufdata->m_bus_rxbuf[5]);
	
	//差错处理
	////size=0或size大于可输出数量
	if((0==(e_mbus_bufdata->mbus_size))||(E_MBUS_Y_SIZE<(e_mbus_bufdata->mbus_size)))
	{
		for_i=e_mbus_bufdata->mbus_size;
		return E_MBUS_ERR_SIZE;
	}
	
	//地址+数量大于可输出数量
	if(E_MBUS_Y_SIZE<(e_mbus_bufdata->mbus_start+e_mbus_bufdata->mbus_size))
	{
		return E_MBUS_ERR_ADDR;
	}
	
	//字节数size
	if(e_mbus_bufdata->mbus_size%8)e_mbus_bufdata->m_bus_txbuf[2]=e_mbus_bufdata->mbus_size/8+1;
	else e_mbus_bufdata->m_bus_txbuf[2]=e_mbus_bufdata->mbus_size/8;
	e_mbus_bufdata->m_bus_txsize=e_mbus_bufdata->m_bus_txbuf[2]+3;//ID+CMD+SIZE+DATASIZE,不包含CRC
	
	//数据整合到输出BUF
	for(for_i=0;for_i<(e_mbus_bufdata->mbus_size);for_i++)
	{
		if(E_MBUS_Y_PIN_SET==E_MBUS_DATA_ALL.mbus_y[for_i+e_mbus_bufdata->mbus_start])
		{
			(e_mbus_bufdata->m_bus_txbuf[for_i/8+3])|=(0x01<<(for_i%8));
		}
		else
		{
			(e_mbus_bufdata->m_bus_txbuf[for_i/8+3])&=(~(0x01<<(for_i%8)));
		}
	}
	
	return E_MBUS_OK;
}


/*02功能码：读连续空间的输入信号状态*----------------------------*/
E_MBUS_StatusTypeDef E_MBUS_X_READ(E_MBUS_BufDataTypeDef *e_mbus_bufdata)
{
	//变量定义
//	E_MBUS_StatusTypeDef recive_status=E_MBUS_ERROR_OTHER;
	E_MBUS_DATA for_i;
	
	//获取起始地址和数据数量
	e_mbus_bufdata->mbus_start=((e_mbus_bufdata->m_bus_rxbuf[2])<<8)|(e_mbus_bufdata->m_bus_rxbuf[3]);
	e_mbus_bufdata->mbus_size= ((e_mbus_bufdata->m_bus_rxbuf[4])<<8)|(e_mbus_bufdata->m_bus_rxbuf[5]);
	
	//差错处理
	////size=0或size大于可输出数量
	if((0==(e_mbus_bufdata->mbus_size))||(E_MBUS_X_SIZE<(e_mbus_bufdata->mbus_size)))
	{
		for_i=e_mbus_bufdata->mbus_size;
		return E_MBUS_ERR_SIZE;
	}
	
	//地址+数量大于可输出数量
	if(E_MBUS_X_SIZE<(e_mbus_bufdata->mbus_start+e_mbus_bufdata->mbus_size))
	{
		return E_MBUS_ERR_ADDR;
	}
	
	//字节数size
	if(e_mbus_bufdata->mbus_size%8)e_mbus_bufdata->m_bus_txbuf[2]=e_mbus_bufdata->mbus_size/8+1;
	else e_mbus_bufdata->m_bus_txbuf[2]=e_mbus_bufdata->mbus_size/8;
	e_mbus_bufdata->m_bus_txsize=e_mbus_bufdata->m_bus_txbuf[2]+3;//ID+CMD+SIZE+DATASIZE,不包含CRC
	
	//数据整合到输出BUF
	for(for_i=0;for_i<(e_mbus_bufdata->mbus_size);for_i++)
	{
		if(E_MBUS_X_PIN_SET==E_MBUS_DATA_ALL.mbus_x[for_i+e_mbus_bufdata->mbus_start])
		{
			(e_mbus_bufdata->m_bus_txbuf[for_i/8+3])|=(0x01<<(for_i%8));
		}
		else
		{
			(e_mbus_bufdata->m_bus_txbuf[for_i/8+3])&=(~(0x01<<(for_i%8)));
		}
	}
	
	return E_MBUS_OK;
}

/*03功能码：读连续空间的读写寄存器数据*----------------------------*/
E_MBUS_StatusTypeDef E_MBUS_WRREG_READ(E_MBUS_BufDataTypeDef *e_mbus_bufdata)
{
	//变量定义
//	E_MBUS_StatusTypeDef recive_status=E_MBUS_ERROR_OTHER;
	E_MBUS_DATA for_i;
	
	//获取起始地址和数据数量
	e_mbus_bufdata->mbus_start=((e_mbus_bufdata->m_bus_rxbuf[2])<<8)|(e_mbus_bufdata->m_bus_rxbuf[3]);
	e_mbus_bufdata->mbus_size= ((e_mbus_bufdata->m_bus_rxbuf[4])<<8)|(e_mbus_bufdata->m_bus_rxbuf[5]);
	
	//差错处理
	//差错地址处理
	
	//读取数据数量判断
	if((0==(e_mbus_bufdata->mbus_size))||(120<(e_mbus_bufdata->mbus_size)))
	{
		return E_MBUS_ERR_ADDR;
	}
	//待读取地址，必须在两个地址段中间
	if((((e_mbus_bufdata->mbus_start)>=E_MBUS_WRREG_ADDR0)&&(((e_mbus_bufdata->mbus_start)+(e_mbus_bufdata->mbus_size))<=(E_MBUS_WRREG_ADDR0+E_MBUS_WRREG_SIZE0))))
	{goto GoTo_WRREG_READSizeRUN;}
	else if((((e_mbus_bufdata->mbus_start)>=E_MBUS_WRREG_ADDR1)&&(((e_mbus_bufdata->mbus_start)+(e_mbus_bufdata->mbus_size))<=(E_MBUS_WRREG_ADDR1+E_MBUS_WRREG_SIZE1))))
	{goto GoTo_WRREG_READSizeRUN;}
	else
	{
		return E_MBUS_ERR_ADDR;
	}
	

	
//	if((e_mbus_bufdata->mbus_start+e_mbus_bufdata->mbus_size)>(E_MBUS_WRREG_ADDR0+E_MBUS_WRREG_SIZE0)
//		||(e_mbus_bufdata->mbus_start+e_mbus_bufdata->mbus_size)>(E_MBUS_WRREG_ADDR1+E_MBUS_WRREG_SIZE1))
//	{
//		for_i=e_mbus_bufdata->mbus_size;
//		return E_MBUS_ERR_SIZE;
//	}
	
	

GoTo_WRREG_READSizeRUN:
	//字节数size
	e_mbus_bufdata->m_bus_txbuf[2]=e_mbus_bufdata->mbus_size*2;
	e_mbus_bufdata->m_bus_txsize=e_mbus_bufdata->m_bus_txbuf[2]+3;//ID+CMD+SIZE+DATASIZE,不包含CRC
	
	//数据整合到输出BUF
	if((E_MBUS_WRREG_ADDR0+E_MBUS_WRREG_SIZE0)>e_mbus_bufdata->mbus_start)
	{
		for(for_i=0;for_i<(e_mbus_bufdata->mbus_size);for_i++)
		{
			e_mbus_bufdata->m_bus_txbuf[for_i*2+3]=(E_MBUS_DATA_ALL.mbus_wrreg0[for_i+e_mbus_bufdata->mbus_start])>>8;
			e_mbus_bufdata->m_bus_txbuf[for_i*2+4]=(E_MBUS_DATA_ALL.mbus_wrreg0[for_i+e_mbus_bufdata->mbus_start])&0xff;
		}
	}
	else if(E_MBUS_WRREG_ADDR1<=e_mbus_bufdata->mbus_start)
	{
		for(for_i=0;for_i<(e_mbus_bufdata->mbus_size);for_i++)
		{
			e_mbus_bufdata->m_bus_txbuf[for_i*2+3]=(E_MBUS_DATA_ALL.mbus_wrreg1[for_i+e_mbus_bufdata->mbus_start])>>8;
			e_mbus_bufdata->m_bus_txbuf[for_i*2+4]=(E_MBUS_DATA_ALL.mbus_wrreg1[for_i+e_mbus_bufdata->mbus_start])&0xff;
		}
	}
	else 
	{
		return E_MBUS_ERR_DATE;
	}
	
	return E_MBUS_OK;
}


/*04功能码：读连续空间的只读寄存器数据*----------------------------*/
E_MBUS_StatusTypeDef E_MBUS_RREG_READ(E_MBUS_BufDataTypeDef *e_mbus_bufdata)
{
	//变量定义
//	E_MBUS_StatusTypeDef recive_status=E_MBUS_ERROR_OTHER;
	E_MBUS_DATA for_i;
	
	//获取起始地址和数据数量
	e_mbus_bufdata->mbus_start=((e_mbus_bufdata->m_bus_rxbuf[2])<<8)|(e_mbus_bufdata->m_bus_rxbuf[3]);
	e_mbus_bufdata->mbus_size= ((e_mbus_bufdata->m_bus_rxbuf[4])<<8)|(e_mbus_bufdata->m_bus_rxbuf[5]);
	
	//差错处理
	////size=0或size大于可输出数量
	if((0==(e_mbus_bufdata->mbus_size))||(E_MBUS_RREG_SIZE<(e_mbus_bufdata->mbus_size)))
	{
		for_i=e_mbus_bufdata->mbus_size;
		return E_MBUS_ERR_SIZE;
	}
	
	//地址+数量大于可输出数量
	if(E_MBUS_RREG_SIZE<(e_mbus_bufdata->mbus_start+e_mbus_bufdata->mbus_size))
	{
		return E_MBUS_ERR_ADDR;
	}
	
	//字节数size
	e_mbus_bufdata->m_bus_txbuf[2]=e_mbus_bufdata->mbus_size*2;
	e_mbus_bufdata->m_bus_txsize=e_mbus_bufdata->m_bus_txbuf[2]+3;//ID+CMD+SIZE+DATASIZE,不包含CRC
	
	//数据整合到输出BUF
	for(for_i=0;for_i<(e_mbus_bufdata->mbus_size);for_i++)
	{
		e_mbus_bufdata->m_bus_txbuf[for_i*2+3]=(E_MBUS_DATA_ALL.mbus_rreg[for_i+e_mbus_bufdata->mbus_start])>>8;
		e_mbus_bufdata->m_bus_txbuf[for_i*2+4]=(E_MBUS_DATA_ALL.mbus_rreg[for_i+e_mbus_bufdata->mbus_start])&0xff;
	}
	
	return E_MBUS_OK;
}


/*05功能码：写单个线圈状态*----------------------------*/
E_MBUS_StatusTypeDef E_MBUS_Y_WRITEonepin(E_MBUS_BufDataTypeDef *e_mbus_bufdata)
{
	//变量定义
//	E_MBUS_StatusTypeDef recive_status=E_MBUS_ERROR_OTHER;
//	E_MBUS_DATA for_i;
	
	//获取起始地址和数据数量
	e_mbus_bufdata->mbus_start=((e_mbus_bufdata->m_bus_rxbuf[2])<<8)|(e_mbus_bufdata->m_bus_rxbuf[3]);
	e_mbus_bufdata->mbus_size= ((e_mbus_bufdata->m_bus_rxbuf[4])<<8)|(e_mbus_bufdata->m_bus_rxbuf[5]);
	
	//差错处理
	////size=0或size大于可输出数量
	if((0!=(e_mbus_bufdata->mbus_size))&&(0xff00!=(e_mbus_bufdata->mbus_size)))
	{
//		for_i=e_mbus_bufdata->mbus_size;
		return E_MBUS_ERR_SIZE;
	}
	
	//地址+数量大于可输出数量
	if(E_MBUS_Y_SIZE<(e_mbus_bufdata->mbus_start))
	{
		return E_MBUS_ERR_ADDR;
	}
	
	//字节数size
	e_mbus_bufdata->m_bus_txsize=4+2;//ID+CMD+DATASIZE,不包含CRC
	
	//数据整合到输出BUF
	{
		if(0xff00==e_mbus_bufdata->mbus_size)
		{
			E_MBUS_DATA_ALL.mbus_y[e_mbus_bufdata->mbus_start]=E_MBUS_Y_PIN_SET;
		}
		else if(0x0000==e_mbus_bufdata->mbus_size)
		{
			E_MBUS_DATA_ALL.mbus_y[e_mbus_bufdata->mbus_start]=E_MBUS_Y_PIN_RESET;
		}
		else
		{
			return E_MBUS_ERR_DATE;
		}
		e_mbus_bufdata->m_bus_txbuf[2]=e_mbus_bufdata->mbus_start>>8;
		e_mbus_bufdata->m_bus_txbuf[3]=e_mbus_bufdata->mbus_start&0xff;
		
		e_mbus_bufdata->m_bus_txbuf[4]=e_mbus_bufdata->mbus_size>>8;
		e_mbus_bufdata->m_bus_txbuf[5]=e_mbus_bufdata->mbus_size&0xff;
	}
	
	return E_MBUS_OK;
}

/*06功能码：写单个寄存器数据*----------------------------*/
E_MBUS_StatusTypeDef E_MBUS_WRREG_WRITEonereg(E_MBUS_BufDataTypeDef *e_mbus_bufdata)
{
	//变量定义
//	E_MBUS_StatusTypeDef recive_status=E_MBUS_ERROR_OTHER;
//	E_MBUS_DATA for_i;
	
	//获取起始地址和数据内容
	e_mbus_bufdata->mbus_start=((e_mbus_bufdata->m_bus_rxbuf[2])<<8)|(e_mbus_bufdata->m_bus_rxbuf[3]);
	e_mbus_bufdata->mbus_size= ((e_mbus_bufdata->m_bus_rxbuf[4])<<8)|(e_mbus_bufdata->m_bus_rxbuf[5]);
	
	//差错处理
	if(
//			((e_mbus_bufdata->mbus_start)<E_MBUS_WRREG_ADDR0)||
			((e_mbus_bufdata->mbus_start<E_MBUS_WRREG_ADDR1)&&((e_mbus_bufdata->mbus_start)>=(E_MBUS_WRREG_ADDR0+E_MBUS_WRREG_SIZE0)))
			||(e_mbus_bufdata->mbus_start>=(E_MBUS_WRREG_ADDR1+E_MBUS_WRREG_SIZE1))
		)
	{
		return E_MBUS_ERR_ADDR;
	}
	//字节数size
	e_mbus_bufdata->m_bus_txsize=4+2;//ID+CMD+DATASIZE,不包含CRC
	
	//数据整合到输出BUF
	{
		if((E_MBUS_WRREG_SIZE0+E_MBUS_WRREG_ADDR0)>(e_mbus_bufdata->mbus_start))
		{
			E_MBUS_DATA_ALL.mbus_wrreg0[e_mbus_bufdata->mbus_start]=e_mbus_bufdata->mbus_size;
		}
		else if((E_MBUS_WRREG_SIZE1+E_MBUS_WRREG_ADDR1)>(e_mbus_bufdata->mbus_start))
		{
			E_MBUS_DATA_ALL.mbus_wrreg1[e_mbus_bufdata->mbus_start]=e_mbus_bufdata->mbus_size;
		}
		else 
		{
			return E_MBUS_ERR_DATE;
		}
			e_mbus_bufdata->m_bus_txbuf[2]=e_mbus_bufdata->mbus_start>>8;
			e_mbus_bufdata->m_bus_txbuf[3]=e_mbus_bufdata->mbus_start&0xff;
			
			e_mbus_bufdata->m_bus_txbuf[4]=e_mbus_bufdata->mbus_size>>8;
			e_mbus_bufdata->m_bus_txbuf[5]=e_mbus_bufdata->mbus_size&0xff;
	}
	
	return E_MBUS_OK;
}


/*15功能码：写多个线圈状态*----------------------------*/
E_MBUS_StatusTypeDef E_MBUS_Y_WRITE(E_MBUS_BufDataTypeDef *e_mbus_bufdata)
{
	//变量定义
//	E_MBUS_StatusTypeDef recive_status=E_MBUS_ERROR_OTHER;
	E_MBUS_DATA for_i;
	
	//获取起始地址和数据数量
	e_mbus_bufdata->mbus_start=((e_mbus_bufdata->m_bus_rxbuf[2])<<8)|(e_mbus_bufdata->m_bus_rxbuf[3]);
	e_mbus_bufdata->mbus_size= ((e_mbus_bufdata->m_bus_rxbuf[4])<<8)|(e_mbus_bufdata->m_bus_rxbuf[5]);
	e_mbus_bufdata->mbus_data_size=(e_mbus_bufdata->m_bus_rxbuf[6]);
	
	//字节数N=size/8；如果余数不为0，则N=N+1
	if(e_mbus_bufdata->mbus_size%8)for_i=e_mbus_bufdata->mbus_size/8+1;
	else for_i=e_mbus_bufdata->mbus_size/8;
	
	//差错处理
	////size=0或size大于可输出数量
	if((0==(e_mbus_bufdata->mbus_size))||(E_MBUS_Y_SIZE<(e_mbus_bufdata->mbus_size))||
			(for_i!=e_mbus_bufdata->mbus_data_size)||(9+for_i!=e_mbus_bufdata->m_bus_rxsize))
	{
		return E_MBUS_ERR_SIZE;
	}
	
	//地址+数量大于可输出数量
	if(E_MBUS_Y_SIZE<(e_mbus_bufdata->mbus_start+e_mbus_bufdata->mbus_size))
	{
		return E_MBUS_ERR_ADDR;
	}
	
	//字节数size
	e_mbus_bufdata->m_bus_txsize=4+2;//ID+CMD+DATASIZE,不包含CRC
	
	//数据输出到MODBUS寄存器
	for(for_i=0;for_i<(e_mbus_bufdata->mbus_size);for_i++)
	{
		if((e_mbus_bufdata->m_bus_rxbuf[for_i/8+7]>>(for_i%8))&0x01)
		{
			E_MBUS_DATA_ALL.mbus_y[for_i+e_mbus_bufdata->mbus_start]=E_MBUS_Y_PIN_SET;
		}
		else{
			E_MBUS_DATA_ALL.mbus_y[for_i+e_mbus_bufdata->mbus_start]=E_MBUS_Y_PIN_RESET;
		}
	}
	
	e_mbus_bufdata->m_bus_txbuf[2]=e_mbus_bufdata->mbus_start>>8;
	e_mbus_bufdata->m_bus_txbuf[3]=e_mbus_bufdata->mbus_start&0xff;
		
	e_mbus_bufdata->m_bus_txbuf[4]=e_mbus_bufdata->mbus_size>>8;
	e_mbus_bufdata->m_bus_txbuf[5]=e_mbus_bufdata->mbus_size&0xff;
	
	return E_MBUS_OK;
}



/*16功能码：写多个寄存器数据*----------------------------*/
E_MBUS_StatusTypeDef E_MBUS_WRREG_WRITE(E_MBUS_BufDataTypeDef *e_mbus_bufdata)
{
	//变量定义
//	E_MBUS_StatusTypeDef recive_status=E_MBUS_ERROR_OTHER;
	E_MBUS_DATA for_i=0;
	
	//获取起始地址和数据数量
	e_mbus_bufdata->mbus_start=((e_mbus_bufdata->m_bus_rxbuf[2])<<8)|(e_mbus_bufdata->m_bus_rxbuf[3]);
	e_mbus_bufdata->mbus_size= ((e_mbus_bufdata->m_bus_rxbuf[4])<<8)|(e_mbus_bufdata->m_bus_rxbuf[5]);
	e_mbus_bufdata->mbus_data_size=(e_mbus_bufdata->m_bus_rxbuf[6]);
	
	//差错处理
	//差错地址处理
	
	//读取数据数量判断
	if((0==(e_mbus_bufdata->mbus_size))||(120<(e_mbus_bufdata->mbus_size)))
	{
		return E_MBUS_ERR_ADDR;
	}
	//待读取地址，必须在两个地址段中间
	if((((e_mbus_bufdata->mbus_start)>=E_MBUS_WRREG_ADDR0)&&(((e_mbus_bufdata->mbus_start)+(e_mbus_bufdata->mbus_size))<=(E_MBUS_WRREG_ADDR0+E_MBUS_WRREG_SIZE0))))
	{goto GoTo_WRREG_WRITESizeRUN;}
	else if((((e_mbus_bufdata->mbus_start)>=E_MBUS_WRREG_ADDR1)&&(((e_mbus_bufdata->mbus_start)+(e_mbus_bufdata->mbus_size))<=(E_MBUS_WRREG_ADDR1+E_MBUS_WRREG_SIZE1))))
	{goto GoTo_WRREG_WRITESizeRUN;}
	else
	{
		return E_MBUS_ERR_ADDR;
	}
	
	
GoTo_WRREG_WRITESizeRUN:
	//字节数size
	e_mbus_bufdata->m_bus_txsize=4+2;//ID+CMD+DATASIZE,不包含CRC
	
	
	//数据输出到MODBUS寄存器
	if((e_mbus_bufdata->mbus_start)<(E_MBUS_WRREG_ADDR0+E_MBUS_WRREG_SIZE0))
	{
		for(for_i=0;for_i<(e_mbus_bufdata->mbus_size);for_i++)
		{
			E_MBUS_DATA_ALL.mbus_wrreg0[for_i+e_mbus_bufdata->mbus_start]=e_mbus_bufdata->m_bus_rxbuf[for_i*2+7]<<8|e_mbus_bufdata->m_bus_rxbuf[for_i*2+8];
		}
	}
	else if((e_mbus_bufdata->mbus_start)<=(E_MBUS_WRREG_ADDR1+E_MBUS_WRREG_SIZE1))
	{
		for(for_i=0;for_i<(e_mbus_bufdata->mbus_size);for_i++)
		{
			E_MBUS_DATA_ALL.mbus_wrreg1[for_i+e_mbus_bufdata->mbus_start-E_MBUS_WRREG_ADDR1]=e_mbus_bufdata->m_bus_rxbuf[for_i*2+7]<<8|e_mbus_bufdata->m_bus_rxbuf[for_i*2+8];
		}
	}
	else
	{
		return E_MBUS_ERR_DATE;
	}
	
	e_mbus_bufdata->m_bus_txbuf[2]=e_mbus_bufdata->mbus_start>>8;
	e_mbus_bufdata->m_bus_txbuf[3]=e_mbus_bufdata->mbus_start&0xff;
		
	e_mbus_bufdata->m_bus_txbuf[4]=e_mbus_bufdata->mbus_size>>8;
	e_mbus_bufdata->m_bus_txbuf[5]=e_mbus_bufdata->mbus_size&0xff;
	
	return E_MBUS_OK;
}

/*MODBUS差错处理*----------------------------*/
E_MBUS_StatusTypeDef E_MBUS_ERR_Recive(E_MBUS_BufDataTypeDef *e_mbus_bufdata,E_MBUS_StatusTypeDef recive_status)
{
//	E_MBUS_StatusTypeDef recive_status=E_MBUS_ERROR_OTHER;
	e_mbus_bufdata->m_bus_txbuf[1]=e_mbus_bufdata->mbus_cmd|0x80;//CMD处理
	e_mbus_bufdata->m_bus_txbuf[2]=recive_status;
	e_mbus_bufdata->m_bus_txsize=3;//ID+CMD+异常码,不包含CRC E_MBUS_ERR_CMD
//	recive_status=E_MBUS_OK;
	return E_MBUS_OK;
}
/*end err_recive*/

/*MODBUS接收数据帧的数据解析*----------------------------*/
/*MODBUS_RTU_HANDLE*/
//只有MODBUS帧不符合当前设备解析情况时返回错误码，如CRC错误，ID错误等；解析数据错误，生成异常帧并返回OK，供主函数返回数据

E_MBUS_StatusTypeDef E_MBUS_HANDLE(E_MBUS_BufDataTypeDef *e_mbus_bufdata)
{
	E_MBUS_StatusTypeDef recive_status=E_MBUS_ERROR_OTHER;
	
	//判断是否NULL
	if(NULL==e_mbus_bufdata->m_bus_txbuf)return E_MBUS_ERROR_OTHER;
	 
	//判断CRC校验
#ifdef MODBUS_HANDLE_CRC_ENABLE
	if(0==ModbusCrcCheck(e_mbus_bufdata->m_bus_rxbuf,e_mbus_bufdata->m_bus_rxsize))
#endif
	{
		e_mbus_bufdata->mbus_id=e_mbus_bufdata->m_bus_rxbuf[0];
		e_mbus_bufdata->mbus_cmd=e_mbus_bufdata->m_bus_rxbuf[1];
		//e_mbus_bufdata->mbus_start=((e_mbus_bufdata->m_bus_rxbuf[2])<<8)|(e_mbus_bufdata->m_bus_rxbuf[3]);
		//e_mbus_bufdata->mbus_size=((e_mbus_bufdata->m_bus_rxbuf[2])<<8)|(e_mbus_bufdata->m_bus_rxbuf[3]);
		
		//判断ID是广播ID或者本机ID，执行MODBUS指令，
		///判断ID//id相等正常相应；ID=0是广播，只执行不回复;ID=SLAVEUNICASTID是单播，执行并用当前ID回复；
//		if((E_MBUS_DATA_ALL.mbus_id==e_mbus_bufdata->mbus_id)||(0==e_mbus_bufdata->mbus_id)||(0xfe==e_mbus_bufdata->mbus_id))
		{
			e_mbus_bufdata->m_bus_txbuf[0]=e_mbus_bufdata->mbus_id;
			e_mbus_bufdata->m_bus_txbuf[1]=e_mbus_bufdata->mbus_cmd;
			e_mbus_bufdata->m_bus_txsize=2;
			switch(e_mbus_bufdata->mbus_cmd)
			{
				case	MBUS_READ_Y:recive_status=E_MBUS_Y_READ(e_mbus_bufdata);break;
				case	MBUS_READ_X:recive_status=E_MBUS_X_READ(e_mbus_bufdata);break;
				case	MBUS_READ_WRREG:recive_status=E_MBUS_WRREG_READ(e_mbus_bufdata);break;
				case 	MBUS_READ_RREG:recive_status=E_MBUS_RREG_READ(e_mbus_bufdata);break;
				case 	MBUS_Y_WriteOnePin:recive_status=E_MBUS_Y_WRITEonepin(e_mbus_bufdata);break;
				case	MBUS_WRREG_WriteOneReg:recive_status=E_MBUS_WRREG_WRITEonereg(e_mbus_bufdata);break;
				case	MBUS_Y_Write:recive_status=E_MBUS_Y_WRITE(e_mbus_bufdata);break;
				case	MBUS_WRREG_Write:recive_status=E_MBUS_WRREG_WRITE(e_mbus_bufdata);break;
				default:recive_status=E_MBUS_ERR_Recive(e_mbus_bufdata,E_MBUS_ERR_CMD);break;
			}
			///数据处理返回异常码
			if(E_MBUS_OK!=recive_status)
			{
				//异常处理
				recive_status=E_MBUS_ERR_Recive(e_mbus_bufdata,recive_status);
			}
			//ID=0，返回其他异常(使用未归类的异常码)
			if(0==e_mbus_bufdata->mbus_id)recive_status=E_MBUS_ERROR_OTHER;
		}
//		else
//		{
//			recive_status=E_MBUS_ERR_ID;
//		}
		return recive_status;
	}

#ifdef MODBUS_HANDLE_CRC_ENABLE
	else
	{return E_MBUS_ERR_CRC;}
#endif
	
}

/*END MODBUS_RTU HANDLE*/








/************************ (C) COPYRIGHT INLEEQ *****END OF FILE****/






